package com.speedricemanager.other.utils;

import android.support.annotation.NonNull;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {
    public static final String MAT="yyyy-MM-dd HH:mm:ss";
    public static final String MAT1="yyyy年MM月dd日 HH:mm";
    public static final String MAT2="yyyy-MM-dd";

    public static String formatCookie(){
        SimpleDateFormat    formatter    =   new SimpleDateFormat(MAT);
        Date    curDate    =   new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String format(){
        SimpleDateFormat    formatter    =   new SimpleDateFormat(MAT1);
        Date    curDate    =   new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String getYear(){
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        return year+"";
    }

    public static String getMonth(){
        Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH);
     return month+"";
    }

    public static String getDay(){
        Calendar c = Calendar.getInstance();
        int  day = c.get(Calendar.DAY_OF_MONTH);
       return day+"";
    }

    public static String getHour(){
        Calendar c = Calendar.getInstance();

        int  hour = c.get(Calendar.HOUR_OF_DAY);
     return hour+"";
    }

    public static String getMinute(){
        Calendar c = Calendar.getInstance();
        int  minute = c.get(Calendar.MINUTE);
        return minute+"";
    }
        //日期转成字符串
    public static String parseString(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String str=sdf.format(date);
        return str;
    }
    //日期转成字符串
    public static String parseString1(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        Date date=new Date();
        String str=sdf.format(date);
        return str;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }


    //获取时间
    @NonNull
    public static String getTime(String time) {
        time=time.replaceAll("-","");
        time=time.substring(0,time.lastIndexOf("."));
        return time;
    }
    //获取时间
    public static String getTimeYMD(String time) {
        time=time.substring(0,time.lastIndexOf(" "));
        return time;
    }

    //某一天 加30天后的日期
public static String dataAdd(String data) {
    try {
    SimpleDateFormat dateFormat = new SimpleDateFormat(MAT2); // 日期格式

        Date   date = dateFormat.parse(data.substring(0,data.indexOf(" ")));
    Date newDate = addDate(date, 30); // 指定日期加上20天
//    System.out.println(dateFormat.format(date));// 输出格式化后的日期
//    System.out.println(dateFormat.format(newDate));
        return  dateFormat.format(newDate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}


    public static Date addDate(Date date,long day) throws ParseException {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        time+=day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    public static long dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return ts;
    }

    //30天后的时间
    public static String dateHome(){
        Date date=new Date();
        SimpleDateFormat s=new SimpleDateFormat("yyyy'年'MM'月'dd'日'");
        System.out.println(s.format(date));
        String str1=s.format(date);//当前的时间
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 30);//计算30天后的时间
        String str2=s.format(c.getTime());
        System.out.println("30天后的时间是："+str2);
        return str2;
    }

}
